<?php

namespace app\admin\model;
use think\Model;
use think\Db;

class MemberModel extends Model
{
    protected $name = 'member';  
    protected $autoWriteTimestamp = true;   // 开启自动写入时间戳

    /**
     * 根据搜索条件获取用户列表信息
     */
    public function getMemberByWhere($map, $Nowpage, $limits)
    {
        return $this->field('think_member.*,offer_name,b.name as province,c.name as city')
            ->join('think_member_offer', 'think_member.offer_id = think_member_offer.id' ,'left')
            ->join('think_area b', 'think_member.home_province = b.id','left')
            ->join('think_area c', 'think_member.home_city = c.id','left')
            ->where($map)->page($Nowpage, $limits)->order('id desc')->select();
    }
    public function getMemberAscByWhere($map, $Nowpage, $limits)
    {
        return $this->field('think_member.*,group_name,think_organization.title as orgname')
        ->join('think_member_group', 'think_member.group_id = think_member_group.id')
        ->join('think_organization', 'think_member_group.organization_id = think_organization.id')
        ->where($map)->page($Nowpage, $limits)->order('orderby asc')->select();
    }
    /**
     * 根据搜索条件获取所有的用户数量
     * @param $where
     */
    public function getAllCount($map)
    {
        return $this
            ->join('think_member_offer', 'think_member.offer_id = think_member_offer.id','left')
            ->join('think_area b', 'think_member.home_province = b.id','left')
            ->join('think_area c', 'think_member.home_city = c.id','left')
            ->where($map)->count();
    }
    /**
     * [getbygroup description]
     * @param  [type] $member_id [description]
     * @return [type]            [description]
     */
    public function getbyOneMember($member_id)
    {
        return $this->where('id',$member_id)->find();
    }

    /**
     * 插入信息
     */
    public function insertMember($param)
    {
        try{
            $result = $this->validate('MemberValidate')->allowField(true)->save($param);
            if(false === $result){            
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '添加成功'];
            }
        }catch( PDOException $e){
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编辑信息
     * @param $param
     */
    public function editMember($param)
    {
        try{
            $result =  $this->allowField(true)->save($param, ['id' => $param['id']]);
            if(false === $result){            
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '编辑成功'];
            }
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }


    /**
     * 根据管理员id获取角色信息
     * @param $id
     */
    public function getOneMember($id)
    {
        return $this->where('id', $id)->find();
    }


    /**
     * 删除管理员
     * @param $id
     */
    public function delUser($id)
    {
        try{

            $this->where('id', $id)->delete();
            Db::name('auth_group_access')->where('uid', $id)->delete();
            return ['code' => 1, 'data' => '', 'msg' => '删除成功'];

        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }


    public function delMember($id)
    {
        try{
            $map['closed']=1;
            $this->save($map, ['id' => $id]);
            return ['code' => 1, 'data' => '', 'msg' => '删除成功'];
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }


}